/*
 * UMLSynch plugin for Eclipse-CDT
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.eclipse.cdt;

import com.googlecode.umlsynch.eclipse.UMLSynchPlugin;
import com.googlecode.umlsynch.eclipse.astutils.ASTUtils;
import com.googlecode.umlsynch.eclipse.astutils.TranslationUnitUtils;
import com.googlecode.umlsynch.rpc.IIndexerRpc;
import com.googlecode.umlsynch.rpc.TClassDescriptor;
import com.googlecode.umlsynch.rpc.TFileDescriptor;
import com.googlecode.umlsynch.rpc.base.IDebug;

public class TCdtIndexerRpc implements IIndexerRpc {
	
//	private IDebug debug = UMLSynchPlugin.getDebug();
	
	private ASTUtils astutils = ASTUtils.instance();
	
	private TranslationUnitUtils tuUtils = TranslationUnitUtils.GetInstance();

	public TCdtIndexerRpc() {
	}

	@Override
	public TClassDescriptor GetClassInfo(TClassDescriptor desc) {
	//	debug.Note("Get Class information.");
		
		if ((desc != null) && (desc.name != null)) {
			TClassDescriptor ret = astutils.IdentifyClass(desc);
			
			if (ret != null) {
		//		debug.Note("Update class");
				ret = tuUtils.UpdateClassData(desc);
			}
			
			return (ret != null) ? ret : desc;
		}

		return desc;
	}

	private String getRelationType(TRelationRpc relationType) {
		switch (relationType) {
		case CLASS_GENERALIZATION:
			return "Generalization";
		case CLASS_BASE:
			return "Base";
		case CLASS_AGGREGATION:
			return "Aggregation";
		case CLASS_REALIZATION:
			return "Realization";
		case CLASS_NESTED:
			return "Nested";
		case CLASS_DEPENDENCY:
			return "Dependency";
		case CLASS_ASSOCIATION:
			return "Association";
		case  CLASS_COMPOSITION:
			return "Composition";			
		}
		return "Unknown";
	}
	
	@Override
	public TClassDescriptor[] GetClassRelations(TClassDescriptor desc,
			TRelationRpc relType, TFileDescriptor[] searchRestrictions) {
		desc = astutils.IdentifyClass(desc);
		if (desc == null) {
			return null;
		}
		String relationType = getRelationType(relType);

		//debug.Note("GetClassRelation" + relationType);

		// Find class relations		
		TClassDescriptor[] result = null;

		if (relationType.equals("Base")) {
			result = astutils.GetClassBases(desc);
		}
		else if (relationType.equals("Generalization")) {
			result = astutils.GetClassGeneralizations(desc);
		}
		else if (relationType.equals("Dependency")
				|| relationType.equals("Aggregation")
				|| relationType.equals("Composition")
				|| relationType.equals("Association")) {
			result = astutils.GetClassRelation(desc, 
					     relationType, searchRestrictions);
		}
		else if (relationType.equals("Nested")
				|| relationType.equals("Containment")) {
			result = astutils.GetNestedClasses(desc);
		}
		else if (relationType.equals("Friend")) {
			result = astutils.GetClassFriends(desc);
		}
		
		return result;
	}
}
